package com.doublefish.doublefishrobot.ui.home.bean;

import android.content.Context;
import android.text.TextUtils;

import com.clj.fastble.data.BleDevice;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者:dugaofeng on 2024/10/16 16:11
 */
public class DeviceBean {

    private int t_id;        //数据的唯一id
    private String name;     //蓝牙的name
    private String mac;      //mac地址 用于蓝牙设备 唯一识别
    private int machineID;   //发球机id 用于对发球指令做二次校验
    private int deviceType;  //设备型号   0.未知设备  1.超8 （超8是第三方公司研发的） 2.超6 （超6是双鱼自己研发的）3.超5
    private String alias;     //设备的别名
    private int protocolType;  //蓝牙协议的类型  0.未知  1.超8 （超8是第三方公司研发的） 2.超6 （超6是双鱼自己研发的） 3.超5

    private BleDevice bleDevice;  //蓝牙设备


    public int getTid() {
        return t_id;
    }

    public void setTid(int t_id) {
        this.t_id = t_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public int getMachineID() {
        return machineID;
    }

    public void setMachineID(int machineID) {
        this.machineID = machineID;
    }

    public int getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(int deviceType) {
        this.deviceType = deviceType;
    }

    public String getAlias() {
        if (TextUtils.isEmpty(alias)) {
            return name;
        }
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public int getProtocolType() {
        if (deviceType == ModeConstant.DeviceTypeS8) {
            return ModeConstant.ProtocolTypeS8;
        } else if (deviceType == ModeConstant.DeviceTypeS6) {
            return ModeConstant.ProtocolTypeS6;
        } else if (deviceType == ModeConstant.DeviceTypeS5
                || deviceType == ModeConstant.DeviceTypeS3) {
            return ModeConstant.ProtocolTypeS5;
        }
        return ModeConstant.ProtocolTypeUnkonw;
    }

    public BleDevice getBleDevice() {
        return bleDevice;
    }

    public void setBleDevice(BleDevice bleDevice) {
        this.bleDevice = bleDevice;
    }

    public String getDeviceTypeName() {
        Context context = AppCacheUtil.getInstance().getAppContext();
        switch (deviceType) {
            case ModeConstant.DeviceTypeS8: {
                return context.getString(R.string.super_8_robot);
            }
            case ModeConstant.DeviceTypeS6: {
                return context.getString(R.string.super_6_robot);
            }
            case ModeConstant.DeviceTypeS5: {
                return context.getString(R.string.super_5_robot);
            }
            case ModeConstant.DeviceTypeS3: {
                return context.getString(R.string.super_3_robot);
            }
            default: {
                return context.getString(R.string.unknown_device);
            }
        }
    }

   public boolean isSameDevice(DeviceBean deviceBean) {
        if (deviceBean == null) {
            return false;
        }
        if (t_id != 0 && t_id== deviceBean.getTid()) {
            return true;
        }
        if (mac != null && mac.equals(deviceBean.getMac())) {
            return true;
        }
        if (name != null && name.equals(deviceBean.getName())) {
            return true;
        }
        return false;
   }

    public boolean isProtocolTypeS6() {
        return getProtocolType() == ModeConstant.ProtocolTypeS6;
    }

    public boolean isProtocolTypeS8() {
        return getProtocolType() == ModeConstant.ProtocolTypeS8;
    }


    public Map<String, Object> toDataBaseMap() {
        Map map = new HashMap();
        if (t_id > 0 ) {
            map.put("t_id", t_id);
        }
        if (!TextUtils.isEmpty(name)) {
            map.put("name", name);
        }
        if (!TextUtils.isEmpty(mac)) {
            map.put("mac", mac);
        }
        map.put("deviceType", deviceType);
        if (!TextUtils.isEmpty(alias)) {
            map.put("alias", alias);
        }
        return map;
    }
}
